package cn.stylefeng.guns.sys.modular.consts.init;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.sys.modular.consts.entity.SysConfig;
import cn.stylefeng.guns.sys.modular.consts.service.SysConfigService;
import cn.stylefeng.guns.sys.modular.system.entity.KmsPool;
import cn.stylefeng.guns.sys.modular.system.service.KmsPoolService;
import cn.stylefeng.guns.sys.modular.system.service.SysQnlKeyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * <p>
 * 参数配置 服务类
 * </p>
 *
 * @author stylefeng
 * @since 2019-06-20
 */
@Component
@Slf4j
public class SysConfigInit implements CommandLineRunner {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private SysQnlKeyService sysQnlKeyService;
    @Autowired
    private KmsPoolService kmsPoolService;

    @Override
    public void run(String... args) throws IOException {

        //初始化所有的常量
        List<SysConfig> list = sysConfigService.list();
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = new FileInputStream(new File(System.getProperty("user.home")+"/.qkd/kms/site.properties"));
        // 使用properties对象加载输入流
        properties.load(in);
        //获取key对应的value值
        String property = properties.getProperty("kms.site.id");
        if (list != null && list.size() > 0) {
            for (SysConfig sysConfig : list) {
                ConstantsContext.putConstant(sysConfig.getCode(), sysConfig.getValue());
            }

            log.info("初始化常量" + list.size() + "条！");
        }
        extracted(property);
//
    }

    private void extracted(String property) {

        String srcPath =System.getProperty("user.home")+"/.qkd/kms/pools";
        boolean isEmpty = FileUtil.isDirEmpty(new File(srcPath));
        if(isEmpty) {
            List<KmsPool> list = kmsPoolService.list();
            if(CollectionUtil.isNotEmpty(list)) {
               for(KmsPool key : list) {
                   System.out.println(kmsPoolService.removeById(key.getId()));
               }
            }
        }
        else {
            File parentfile = new File(srcPath);
            File[] files = parentfile.listFiles();
            for (File file : files) {
                String srcName = FileUtil.getName(file);
                File[] sunFileList = file.listFiles();
                for(File sunFile : sunFileList) {
                    //密钥块
                    File[] listKeyBack = sunFile.listFiles();
                    String destName = FileUtil.getName(sunFile);
                    for(File listKey : listKeyBack) {
                        String backId = FileUtil.getName(listKey);
                        QueryWrapper<KmsPool> srcwrapper = new QueryWrapper<>();
                        srcwrapper.eq("back_id",backId);
                        KmsPool one = kmsPoolService.getOne(srcwrapper);
                        if(one == null) {
                            List<String> strings = FileUtil.readLines(listKey.getAbsolutePath(), "UTF-8");
                            String join = CollectionUtil.join(strings, ",");
                            KmsPool kmsPool = new KmsPool();
                            kmsPool.setBackId(backId);
                            kmsPool.setDestSite(destName);
                            kmsPool.setSrcSite(srcName);
                            kmsPool.setFilePath(listKey.getAbsolutePath());
                            kmsPool.setKey(join);
                            kmsPoolService.save(kmsPool);
                        }

                    }
                }
            }
        }
//        //初始化秘钥数据
//        File file = new File(System.getProperty("user.home")+"/.qkd/qnl/qll/keys");
//        File[] files = file.listFiles();
//        for (File keyFile :files) {
//            File[] files1 = keyFile.listFiles();
//            for (File keyFile2 :files1) {
//                FileReader reader = new FileReader(keyFile2);
//                List<String> strings = reader.readLines();
//                String name = keyFile2.getName();
//                for (String str :strings) {
//                    SysQnlKey sysQnlKey = new SysQnlKey();
//                    sysQnlKey.setKey(str);
//                    sysQnlKey.setDestSite(name);
//                    sysQnlKey.setSrcSite(property);
//                    sysQnlKey.setPoolSize(4096);
//                    sysQnlKey.setStatus("正常");
//                    sysQnlKeyService.save(sysQnlKey);
//                }
//            }
//        }
    }
}
